"""
Case Type   : 系统内部使用工具
Case Name   : 执行初始化数据库命令gs_ctl init：缺少必填项
Create At   : 2022-07-28
Owner       : liu-tong-8848
Description :
    1.执行gs_ctl init的命令,缺少必填项
    2.未生成初始化的目录
Expect      :
    1.失败
    2.成功
History     :
"""

import unittest
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro
import os

class Tools(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.userNode = Node('PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.dir_path = os.path.join(macro.DB_INSTANCE_PATH, 'datadir')
        self.nodename = 'nodename_139'

    def test_systools(self):
        self.logger.info(f'-----{os.path.basename(__file__)}' + ' start-----')

        text = '--step1:执行gs_ctl init的命令,缺少必填项；expect：失败--'
        self.logger.info(text)
        excute_cmd1 = f'source {self.DB_ENV_PATH};' \
                      f'gs_ctl init -D {self.dir_path}'
        self.logger.info(excute_cmd1)
        msg1 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg1)
        self.assertIn('node name is mandatory' and 'failed',
                      msg1, text + '执行失败')

        text = '--step2:未生成初始化的目录；expect：成功--'
        self.logger.info(text)
        excute_cmd2 = f'ls {self.dir_path}'
        self.logger.info(excute_cmd2)
        msg2 = self.userNode.sh(excute_cmd2).result()
        self.logger.info(msg2)
        self.assertIn('No such file or directory', msg2, text + '执行失败')

    def tearDown(self):
        self.logger.info(
            f'-----{os.path.basename(__file__)}' + ' end-----')


